//
//  TravellvxingdiCollectionViewCell.m
//  ToWhere
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravellvxingdiCollectionViewCell.h"
#import "DataAnalysis.h"
#import <MJRefresh.h>
#import "TravelLXDCollectionViewCell.h"
#import "TravetTourDetailsViewController.h"
@interface TravellvxingdiCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) MJRefreshNormalHeader *header;

@property (nonatomic, retain) MJRefreshAutoGifFooter *footer;

@property (nonatomic, retain) NSMutableArray *mainArray;

@property (nonatomic, assign) NSInteger DataNumber;

@end

@implementation TravellvxingdiCollectionViewCell

- (void)dealloc
{
    [_searchName release];
    [_collectionView release];
    [_header release];
    [_footer release];
    [_mainArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        //初始化数组
        [self initArray];
        //解析数据
        [self gainData];
        //初始化collectionView
        [self initcollectionView];
        
        self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Refresh)];
        
        self.header.lastUpdatedTimeLabel.hidden = YES;
        
        self.collectionView.header = self.header;
        self.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        
        [self.footer setTitle:@"Loading more ..." forState:MJRefreshStateRefreshing];
        
        self.collectionView.footer = self.footer;
    }
    return self;
}
#pragma init
- (void)initArray{
    self.mainArray = [NSMutableArray array];
    
}
#pragma mark 刷新
- (void)Refresh{
    
    [self.mainArray removeAllObjects];
    _DataNumber = 1;
    [self.collectionView reloadData];
    [self gainData];
    
}
- (void)loadMoreData{
    
    
    _DataNumber += 1;
    
    if(_DataNumber <= 5){
        
        [self gainData];
    }
    else
        [self.footer noticeNoMoreData];
}


- (void)gainData{

    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/search/attractions.json?q=%@&page=%ld",self.searchName,_DataNumber];
    urlString =  [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [DataAnalysis dataAnalysisGETWithUrlString:urlString parameters:self showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
        
        NSArray *array = responseObject;
        for (NSDictionary *dic in array) {
            if ([[dic objectForKey:@"image_url"] isKindOfClass:[NSNull class]]) {
                continue;
            }
            [self.mainArray addObject:dic];
        }
      
        [self.collectionView reloadData];
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        
    } failureBlock:^(NSError *error) {
        
    }];
}
- (void)initcollectionView{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake(177 *kWidth , 125 *kHeight);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0 *kWidth , 375 *kWidth , 567 *kHeight) collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    self.collectionView.scrollEnabled = YES;
    
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.896 alpha:1.000];
     [self.collectionView registerClass:[TravelLXDCollectionViewCell class] forCellWithReuseIdentifier:@"TravelLXDCollectionViewCell"];
    
    [self.contentView addSubview:self.collectionView];
   
    
    [layout release];
    [_collectionView release];
    
}
#pragma mark collectionView代理回调
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(5 *kWidth, 5 *kHeight, 5 *kWidth, 5 *kHeight);
}
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.mainArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    TravelLXDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TravelLXDCollectionViewCell" forIndexPath:indexPath];
    cell.dic = self.mainArray[indexPath.row];
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

//    
//    travel.idNumber = [self.mainArray[indexPath.row] objectForKey:@"id"];
    [self.delegate sender:[self.mainArray[indexPath.row] objectForKey:@"id"]];
}
#pragma mark 重写set方法
- (void)setSearchName:(NSString *)searchName{
    
    if (_searchName != searchName) {
        [_searchName release];
        _searchName = [searchName copy];
        
        [self.collectionView.header beginRefreshing];
    }
}
@end
